import { localCache } from '@/utils/cache'
import { createRouter, createWebHashHistory } from 'vue-router'
import { LOGIN_TOKENS } from '@/global/constants'
import { firstMenu } from '@/utils/map-menus'

const router = createRouter({
  // 哈希
  history: createWebHashHistory(),
  // 映射关系：path => component
  routes: [
    {
      path: '/',
      redirect: '/main'
    },
    {
      path: '/login',
      component: () => import('../views/login/login.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('../views/main/main.vue')
      // children: [
      //   {
      //     path: '/main/1home',
      //     component: () => import('@/views/main/1home/home.vue')
      //   },
      //   {
      //     path: '/main/2supervisory/trajectory',
      //     component: () => import('@/views/main/2supervisory/trajectory/trajectory.vue')
      //   },
      //   {
      //     path: '/main/2supervisory/video',
      //     component: () => import('@/views/main/2supervisory/video/video.vue')
      //   },
      //   {
      //     path: '/main/3personnel/distribute',
      //     component: () => import('@/views/main/3personnel/distribute/distribute.vue')
      //   },
      //   {
      //     path: '/main/3personnel/examine',
      //     component: () => import('@/views/main/3personnel/examine/examine.vue')
      //   },
      //   {
      //     path: '/main/4quest',
      //     component: () => import('@/views/main/4quest/quest.vue')
      //   },
      //   {
      //     path: '/main/5report',
      //     component: () => import('@/views/main/5report/report.vue')
      //   },
      //   {
      //     path: '/main/6system/department',
      //     component: () => import('@/views/main/6system/department/department.vue')
      //   },
      //   {
      //     path: '/main/6system/menu',
      //     component: () => import('@/views/main/6system/menu/menu.vue')
      //   },
      //   {
      //     path: '/main/6system/role',
      //     component: () => import('@/views/main/6system/role/role.vue')
      //   },
      //   {
      //     path: '/main/6system/user',
      //     component: () => import('@/views/main/6system/user/user.vue')
      //   }
      // ]
    },
    {
      path: '/:pathMatch(.*)',
      component: () => import('../views/not-found/notFound.vue')
    }
  ]
})
router.beforeEach((to, from) => {
  const token = localCache.getCache(LOGIN_TOKENS)
  if (to.path === '/main' && !token) {
    return '/login'
  }
  if (to.path === '/main') {
    return firstMenu?.url
  }
})

export default router
